import json

__all__ = ['init_graph', 'save_graph']

def init_graph(node_list, edges):
    """
    利用节点信息和边信息构建一个图。
    :param node_list: 节点列表
    :param edges: 边列表，元素格式为"start end weight"
    :return: dict, 分别存储节点信息和边信息
    """

    return {"vertices": node_list, "edges": edges}


def save_graph(graph, json_path):
    """
    序列化图信息
    :param json_path: json文件的保存路径
    :param graph: 图对象
    :return:
    """
    with open(json_path, "w", encoding='utf8') as f:
        json.dump(graph, f)


def load_graph(json_path):
    """
    加载图的序列化文件
    :param json_path: json文件的保存路径
    :return: dict, 图对象
    """

    with open(json_path, "r", encoding='utf8') as f:
        graph = json.load(f)

    return graph
